package com.weeego.web.action;

import com.sql.weeego.bean.base.CenterConfig;
import com.weeego.web.base.BaseAction;
import com.weeego.web.base.Page;
import java.util.HashMap;
import java.util.Map;

public class AdminConfigAction extends BaseAction {

	public AdminConfigAction() {
		super();
		this.objectForm = new CenterConfig();
	}

	Map msg = new HashMap();
	String ids = "";
	String key = "";
	String value = "";
	String info = "";

	@Override
	public String action() {

		if (this.getAct() == 1) {
			if ("".equals(key)) {
				this.commondResult = getText("admin.config.key.is.null");
			} else {
				// check the key
				if (this.service
						.findByHql(
								" from CenterConfig t where t.centerKey ='"
										+ key + "'").size() > 0) {
					// this.commondResult = getText("admin.config.key.is.here");
					CenterConfig _CenterConfig = new CenterConfig(key, value,
							info);
					if (id > 0) {
						_CenterConfig.setId(id);
					} else {
						this.commondResult = getText("admin.config.key.is.here");
						return "addsuccess";
					}
					this.objectForm = _CenterConfig;
					this.save();
					this.commondResult = getText("admin.config.editok");
				} else {
					CenterConfig _CenterConfig = new CenterConfig(key, value,
							info);
					// the key is change
					if (id > 0) {
						_CenterConfig.setId(id);
						this.commondResult = getText("admin.config.editok");
					} else {
						this.commondResult = getText("admin.config.ok");
					}
					this.objectForm = _CenterConfig;
					this.save();

				}
			}
			return "addsuccess";
		} else if (this.getAct() == 2) {
			// del

			if ("".equals(ids)) {
				// do nothing
			} else {
				// del
				this.commondResult = getText("admin.config.del.ok");
				this.service.deleteById(new CenterConfig(), ids);
			}
			return "delsuccess";
		} else if (this.getAct() == 3) {
			// read message and set it as json
			CenterConfig _CenterConfig = new CenterConfig();
			if (id > 0) {
				_CenterConfig = (CenterConfig) this.service.findObjectById(
						new CenterConfig(), id);
			}
			this.commondResult = _CenterConfig.toJson();
			return "editsuccess";
		} else {

			// List _a = new ArrayList();
			this.hql = "from CenterConfig t order by t.centerKey";
			Page _page = new Page();
			pagesize = 12;
			_page = findPage(hql, getNowpage(), getPagesize());
			// _a =
			// this.service.findByHql("from CenterConfig t order by t.centerKey");
			msg.put("list", _page);

			return SUCCESS;
		}
	}

	public Map getMsg() {
		return msg;
	}

	public void setMsg(Map msg) {
		this.msg = msg;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
}
